<template>
  <div class="buttheight">
    <div>
      <picture-view></picture-view>
      <div class="goodsinfo">
        <div class="goodInfo box-border">
          <div class="goodInfo-title">{{shopCart.title}}</div>
          <div class="price">
            市场价:
            <span class="price-sc">{{shopCart.market_price}}</span>
            销售价:
            <span class="price-xs">{{shopCart.sell_price}}</span>
          </div>
          <div class="count">
            <span>购买数量:</span>
            <van-stepper v-model="value" integer />
          </div>
          <div class="butt">
            <van-button type="info">立即购买</van-button>
            <van-button type="danger" @click="addShow">加入购物车</van-button>
          </div>
        </div>
        <div class="box-border goodParams">
          <div class="goodParams-title">商品参数</div>
          <div class="goodParams-spcs">
            <p>商品货号:{{shopCart.goods_no}}</p>
            <p>库存情况: {{shopCart.stock_quantity}}件</p>
            <p>上架时间: {{shopCart.add_time | dateFormat}}</p>
          </div>
          <div class="btns">
            <van-button plain type="info" class="btns-tpjs" to="goodsrecommend">图文介绍</van-button>
            <van-button plain type="danger" class="btns-tpjs">商品评论</van-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import { Toast } from 'vant'
import { mapState, mapMutations } from 'vuex'
Vue.use(Toast)

export default {
  data () {
    return {
      images: [],
      shopCart: {},
      value: 0,
      tmp: [],
      swipeImg: []
    }
  },
  computed: {
    ...mapState(['goodsListId', 'cartList'])
  },
  created () {
    this.getCartList()
  },
  methods: {
    ...mapMutations(['addCarItem', 'updateCartItem', 'setPicture']),
    getCartList () {
      this.$http.get('api/getthumimages/' + this.goodsListId).then(data => {
        console.log(data.data.message)
        this.images = data.data.message
        data.data.message.forEach(item => this.swipeImg.push(item.src))
        this.setPicture(this.swipeImg)
      })
      this.$http.get('/api/goods/getinfo/' + this.goodsListId).then(data => {
        console.log(data.data.message)
        this.shopCart = data.data.message[0]
      })
    },
    addShow () {
      Toast.success('成功添加')
      console.log(this.cartList)
      this.tmp = this.cartList
      let index = this.tmp.findIndex(item => item.id === this.shopCart.id)
      if (index !== -1) {
        this.tmp.forEach(item => {
          if (item.id === this.shopCart.id) {
            item.cou += this.value
            item.thumb_path = this.images[0].src
          }
        })
        this.updateCartItem(this.tmp)
        return
      }

      // this.shopCar = this.shopCar + this.value
      this.shopCart.cou = this.value
      this.shopCart.thumb_path = this.images[0].src
      this.addCarItem(this.shopCart)
    }
  }
}
</script>
<style lang="less" scoped>
.buttheight {
  margin-bottom: 60px;
  margin-top: 47px;
}
.focus {
  height: 260px;
  margin-top: 50px;
  text-align: center;
  img {
    width: 50%;
  }
}
input {
  width: 45px;
}
.goodInfo {
  .goodInfo-title {
    margin: 15px;
    font-size: 16px;
    line-height: 30px;
    border-bottom: 1px solid #ccc;
  }
  .price {
    margin: 15px 20px;
    color: #8f8f94;

    .price-sc {
      text-decoration: line-through;
      margin-right: 20px;
    }
    .price-xs {
      color: #f00;
      font-weight: bold;
    }
  }
  .count {
    height: 35px;
    margin: 15px;
    color: #8f8f94;

    span {
      float: left;
      line-height: 35px;
      margin-right: 15px;
    }
    .van-stepper-minu {
      float: left;
      box-sizing: border-box;
      border: 0;

      width: 35px;
      height: 35px;
      background: #f2f3f5;
      font-size: 20px;
      color: #333;
      line-height: 35px;
    }
    .van-stepper-input {
      float: left;
      width: 45px;
      height: 33px;
      background: #f2f3f5;
      border: 0;
      margin: 0 2px;
      text-align: center;
      color: #333;
    }
  }
  .butt {
    width: 100%;

    margin: 15px;
  }
}
.box-border {
  margin: 10px;
  border: 1px solid #d9d9d9;
  border-radius: 5px;
}
.van-button {
  height: 38px;
  line-height: 38px;
  margin-right: 10px;
}
.goodParams {
  .goodParams-title {
    margin: 10px 15px;
    font-size: 16px;
    line-height: 40px;
    border-bottom: 1px solid #ccc;
  }
  .goodParams-spcs {
    margin: 15px;
    border-bottom: 1px solid #d9d9d9;
    p {
      color: #8f8f94;
      margin: 10px;
      font-size: 16px;
    }
  }
  .btns {
    margin: 15px;
    .btns-tpjs {
      width: 100%;
      text-align: center;
      margin-bottom: 15px;
    }
  }
}
</style>
